package com.bai.localshop.controller.admin;

import com.bai.localshop.pojo.dto.ProductCategoryPageDTO;
import com.bai.localshop.pojo.dto.ProductCategorySaveDTO;
import com.bai.localshop.pojo.dto.ProductCategoryUpdateDTO;
import com.bai.localshop.pojo.dto.ProductUpdateStatusDTO;
import com.bai.localshop.pojo.vo.ProductCategoryOptionVO;
import com.bai.localshop.pojo.vo.ProductCategoryPageVO;
import com.bai.localshop.pojo.vo.ProductCategoryVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.ProductCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类管理控制器
 */
@RestController
@RequestMapping("/api/admin/product/categories")
@RequiredArgsConstructor
@Slf4j
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;

    /**
     * 获取商品分类列表（用于下拉选择）
     */
    @GetMapping("/options")
    public Result<List<ProductCategoryOptionVO>> listCategoryOptions() {
        return Result.success(productCategoryService.listCategoryOptions());
    }

    /**
     * 分页查询商品分类
     */
    @GetMapping("/page")
    public Result<IPage<ProductCategoryPageVO>> pageQuery(ProductCategoryPageDTO requestParam) {
        return Result.success(productCategoryService.pageQuery(requestParam));
    }

    /**
     * 查询所有启用的商品分类
     */
    @GetMapping("/enabled")
    public Result<List<ProductCategoryOptionVO>> listEnabledCategories() {
        return Result.success(productCategoryService.listEnabledCategories());
    }

    /**
     * 根据ID查询商品分类（回显数据）
     */
    @GetMapping("/{id}")
    public Result<ProductCategoryVO> getCategoryById(@PathVariable Long id) {
        return Result.success(productCategoryService.getCategoryById(id));
    }

    /**
     * 修改商品分类
     */
    @PutMapping("/update")
    public Result<String> updateCategory(@RequestBody ProductCategoryUpdateDTO requestParam) {
        productCategoryService.updateCategory(requestParam);
        return Result.success("修改成功");
    }

    /**
     * 删除商品分类（如果分类下有商品，则不能删除）
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCategoryById(@PathVariable Long id) {
        productCategoryService.deleteCategoryById(id);
        return Result.success("删除成功");
    }

    /**
     * 新增商品分类
     */
    @PostMapping
    public Result<String> saveCategory(@RequestBody ProductCategorySaveDTO requestParam) {
        productCategoryService.saveCategory(requestParam);
        return Result.success("新增成功");
    }

    /**
     * 修改商品分类状态
     */
    @PutMapping("/update/status")
    public Result<String> updateStatus(@RequestBody ProductUpdateStatusDTO requestParam) {
        productCategoryService.updateStatus(requestParam);
        return Result.success("修改成功");
    }
}